package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.Util.TreeNodeBuilder;
import com.erp.Util.WebUtil;
import com.erp.comm.Code;
import com.erp.comm.DataAndView;
import com.erp.comm.TreeNode;
import com.erp.pojo.Permission;
import com.erp.pojo.UserInfo;
import com.erp.service.PermissionService;
import com.erp.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {


    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("loadIndexLeftMenuJson")
    public DataAndView loadIndexLeftMenu() {
        //获取当前登录的用户
        UserInfo user = (UserInfo) WebUtil.getSession().getAttribute("user");

        //判断用户类型
        List<Permission> list = null;
        if (user.getType() == Code.USER_SUPER) {
            //超级用户  查询所有type为menu的数据

            //设置查询条件
            QueryWrapper<Permission> qw1 = new QueryWrapper<>();
            qw1.eq("type", Code.PERMISSION_MENU);//菜单项
            qw1.eq("available", Code.AVALIABLE);//可用
            list = permissionService.list(qw1);

        } else {

            //普通用户：根据uid --》rid --》pid--》permission
            //uid
            int uid = user.getId();

            //根据uid --》rid
            List<Integer> listrids = roleService.getRoleByUid(uid);
            //存储符合要求的Permission对象
//            List<Permission> pss = null;

            //根据rid查pid
            //去重
            HashSet<Integer> spids = new HashSet<>();
            for (Integer rid : listrids) {

                //每个角色对应的pid集合
                List<Integer> pids = permissionService.getPidByRid(rid);
                //根据角色查询出来pid存在重复
                System.out.println(pids);
                //去重
                spids.addAll(pids);  //将集合中的元素添加到set集合中
            }
            //根据pid在sys_permission中，查type为menu的数据
            QueryWrapper<Permission> qw1 = new QueryWrapper<>();
            qw1.eq("type", Code.PERMISSION_MENU);//菜单项
            qw1.eq("available", Code.AVALIABLE);//可用
            qw1.in("id", spids);//指定id范围查询

            //该用户所对应的menu权限菜单
            list = permissionService.list(qw1);

            System.out.println(listrids);
            System.out.println(spids);

        }


//        System.out.println(list);
//        System.out.println(user);

        //定义空的节点集合
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        //遍历当前用户的permission集合

        for (Permission p : list) {
            //id
            Integer id = p.getId();
            Integer pid = p.getPid();
            String title = p.getTitle();
            String icon = p.getIcon();
            String herf = p.getHref();
            Boolean spread = (p.getOpen() == 1) ? true : false; //1 展开  0  不展开

            TreeNode node = new TreeNode(id, title, herf, spread, icon, pid);
            treeNodes.add(node);

        }
        System.out.println(treeNodes);

        //构建层级关系
        List<TreeNode>list1 = TreeNodeBuilder.Builder(treeNodes,1);
        return new DataAndView(list1);


    }
}


